﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class AreaDetailDA : IAreaDetailDA
    {
        private string AREA_ID = "@AREA_ID";
        private string CREATED_BY = "@CREATED_BY";
        private string SEQ_NO = "@SEQ_NO";
        private string SQL_CHECK_AREA_DETAIL_ID_UNIQUE = " SELECT COUNT(1) FROM @_@AREA_DETAIL WHERE AREA_ID = @AREA_ID AND ZONE_ID = @ZONE_ID ";
        private string SQL_DELETE_AREA_DETAIL = " DELETE FROM @_@AREA_DETAIL WHERE AREA_ID = @AREA_ID AND ZONE_ID = @ZONE_ID ";
        private string SQL_DELETE_AREA_DETAIL_BY_ARAD_ID = " DELETE FROM @_@AREA_DETAIL WHERE AREA_ID = @AREA_ID ";
        private string SQL_INSERT_AREA_DETAIL = " INSERT INTO @_@AREA_DETAIL ( WH_ID, AREA_ID, ZONE_ID, SEQ_NO, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @AREA_ID, @ZONE_ID, @SEQ_NO, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_AREA_DETAIL = " SELECT  ROWID, WH_ID, AREA_ID, ZONE_ID, SEQ_NO, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@AREA_DETAIL ";
        private string SQL_UPDATE_AREA_DETAIL = " UPDATE @_@AREA_DETAIL SET WH_ID = @WH_ID, SEQ_NO = @SEQ_NO, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE AREA_ID = @AREA_ID AND ZONE_ID = @ZONE_ID  ";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";
        private string ZONE_ID = "@ZONE_ID";

        public bool CheckAreaDetailIDUnique(string areaID, string zoneID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@AREA_ID", areaID), new SqlParameter("@ZONE_ID", zoneID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_AREA_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckAreaDetailIDUnique(DataBase dataBase, DbTransaction tran, string areaID, string zoneID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@AREA_ID", areaID), new SqlParameter("@ZONE_ID", zoneID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_AREA_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteAreaDetail(List<string> areaID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < areaID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@AREA_ID", areaID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_AREA_DETAIL_BY_ARAD_ID.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteAreaDetail(List<string> areaID, List<string> zoneID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    if (areaID.Count != zoneID.Count)
                    {
                        throw new Exception("AreaID和ZoneID数量不对应!");
                    }
                    for (int j = 0; j < areaID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@AREA_ID", areaID[j]), new SqlParameter("@ZONE_ID", zoneID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_AREA_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteAreaDetail(DataBase dataBase, DbTransaction tran, List<string> areaID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < areaID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@AREA_ID", areaID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_AREA_DETAIL_BY_ARAD_ID.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteAreaDetail(DataBase dataBase, DbTransaction tran, List<string> areaID, List<string> zoneID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    if (areaID.Count != zoneID.Count)
                    {
                        throw new Exception("AreaID和ZoneID数量不对应!");
                    }
                    for (int j = 0; j < areaID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@AREA_ID", areaID[j]), new SqlParameter("@ZONE_ID", zoneID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_AREA_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllAreaDetail(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_AREA_DETAIL.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllAreaDetail(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_AREA_DETAIL.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public List<AreaDetailInfo> GetAreaDetailByAreaID(string areaID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_AREA_DETAIL.Replace("@_@", whLoginID) + " WHERE AREA_ID = @AREA_ID ORDER BY SEQ_NO ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter(this.AREA_ID, areaID) };
            List<AreaDetailInfo> areaDetailInfo = new List<AreaDetailInfo>();
            AreaDetailInfo areaDetail = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    areaDetail = new AreaDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["AREA_ID"].ToString(), reader["ZONE_ID"].ToString(), (reader["SEQ_NO"].ToString() != "") ? int.Parse(reader["SEQ_NO"].ToString()) : 0, reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                    areaDetailInfo.Add(areaDetail);
                }
            }
            return areaDetailInfo;
        }

        public List<AreaDetailInfo> GetAreaDetailByAreaID(DataBase dataBase, DbTransaction tran, string areaID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_AREA_DETAIL.Replace("@_@", whLoginID) + " WHERE AREA_ID = @AREA_ID ORDER BY SEQ_NO ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter(this.AREA_ID, areaID) };
            List<AreaDetailInfo> areaDetailInfo = new List<AreaDetailInfo>();
            AreaDetailInfo areaDetail = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            while (reader.Read())
            {
                DateTime CS00002;
                areaDetail = new AreaDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["AREA_ID"].ToString(), reader["ZONE_ID"].ToString(), (reader["SEQ_NO"].ToString() != "") ? int.Parse(reader["SEQ_NO"].ToString()) : 0, reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                areaDetailInfo.Add(areaDetail);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return areaDetailInfo;
        }

        public AreaDetailInfo GetAreaDetailByID(string areaID, string zoneID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_AREA_DETAIL.Replace("@_@", whLoginID) + " WHERE AREA_ID = @AREA_ID AND ZONE_ID = @ZONE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@AREA_ID", areaID), new SqlParameter("@ZONE_ID", zoneID) };
            AreaDetailInfo areaDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    areaDetailInfo = new AreaDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["AREA_ID"].ToString(), reader["ZONE_ID"].ToString(), (reader["SEQ_NO"].ToString() != "") ? int.Parse(reader["SEQ_NO"].ToString()) : 0, reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return areaDetailInfo;
        }

        public AreaDetailInfo GetAreaDetailByID(DataBase dataBase, DbTransaction tran, string areaID, string zoneID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_AREA_DETAIL.Replace("@_@", whLoginID) + " WHERE AREA_ID = @AREA_ID AND ZONE_ID = @ZONE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@AREA_ID", areaID), new SqlParameter("@ZONE_ID", zoneID) };
            AreaDetailInfo areaDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                areaDetailInfo = new AreaDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["AREA_ID"].ToString(), reader["ZONE_ID"].ToString(), (reader["SEQ_NO"].ToString() != "") ? int.Parse(reader["SEQ_NO"].ToString()) : 0, reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return areaDetailInfo;
        }

        public DataSet GetAreaDetailByQueryList(List<string> sqlWhere, AreaQueryEntity areadetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_AREA_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!areadetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, areadetailQuery.CurrentPage, areadetailQuery.PageSize, areadetailQuery.SortField, areadetailQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetAreaDetailByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, AreaQueryEntity areadetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_AREA_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!areadetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, areadetailQuery.CurrentPage, areadetailQuery.PageSize, areadetailQuery.SortField, areadetailQuery.SortDirection);
            }
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertAreaDetail(AreaDetailInfo areaDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    areaDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_AreaDetail_Parameters(areaDetailInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_AREA_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertAreaDetail(DataBase dataBase, DbTransaction tran, AreaDetailInfo areaDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    areaDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_AreaDetail_Parameters(areaDetailInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_AREA_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_AreaDetail_Parameters(AreaDetailInfo areaDetailInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.AREA_ID, SqlDbType.VarChar, 20), new SqlParameter(this.ZONE_ID, SqlDbType.VarChar, 20), new SqlParameter(this.SEQ_NO, SqlDbType.Int, 4), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(areaDetailInfo.WhID))
            {
                paramArray[0].Value = areaDetailInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = areaDetailInfo.AreaID;
            paramArray[2].Value = areaDetailInfo.ZoneID;
            paramArray[3].Value = areaDetailInfo.SeqNo;
            paramArray[4].Value = areaDetailInfo.CreatedBy;
            paramArray[5].Value = areaDetailInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateAreaDetail(AreaDetailInfo areaDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    areaDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_AreaDetail_Parameters(areaDetailInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_AREA_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateAreaDetail(DataBase dataBase, DbTransaction tran, AreaDetailInfo areaDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    areaDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_AreaDetail_Parameters(areaDetailInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_AREA_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

